// pages/scan/scan.js
const app = getApp();
var self;
var username,password,code;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    result: '',
    shangjiInfo:null
  },

  goScan: function () {
    // 允许从相机和相册扫码
    wx.scanCode({
      success: (res) => {
        
        var result = res.result.split('|');
        console.log(result)  // 993A16D2-9A7C-4BBC-9FB6-82C9E0CB1421|0|0
        if (result[0] == '1'){//仪器上机
          console.log(wx.getStorageSync('user'))
          var user = wx.getStorageSync('user') == "" ? -999 : wx.getStorageSync('user');
          if(user==-999){
            wx.navigateTo({
              url: '/pages/login/login',
            })
          }else{
            username = user.username;
            password = user.password;
            code = result[1];
            self.getShangji();
          }

        } else if(result[0] == '2'){ //仪器预约
          if (result[2] == '0'){
            wx.navigateTo({
              url: '/pages/indexAll/yiqiStep1/yiqiStep1?id=' + result[1],
            })
          } else if (result[2] == '1'){
            wx.navigateTo({
              url: '/pages/indexAll/songyangStep1/songyangStep1?id=' + result[1],
            })
          }
        } else if(result[0] == '3'){ //实验室预约
          wx.navigateTo({
            url: '/pages/indexAll/shiyanStep/shiyanStep?id=' + result[1],
          })
        }
      }
    })
  },
  getShangji:function(){
    if(this.data.shangjiInfo!=null){
        wx.showToast({
          title: '您尚未下机，请下机后再扫码',
          icon:'none'
        })
        return false;
    }
    wx.request({
      url: app.globalData.apiUrl + "IS00/Login.ashx?Method=IdentityCheck",
      data: {
        LoginCode: username,
        LoginPassword: password,
        DeviceCode: code
      },
      method: 'GET',
      success: function (res) {
        console.log(res);
        if(res.data.state==1){
          var shangjiInfo = res.data.retData;
          var myDate = new Date();
          var hour = myDate.getHours() < 10 ? ("0" + myDate.getHours()) : myDate.getHours();
          var minutes = myDate.getMinutes() < 10 ? ("0" + myDate.getMinutes()) : myDate.getMinutes();
          console.log(hour,minutes);
          shangjiInfo.date = (myDate.getMonth() + 1) + "-" + myDate.getDate();
          shangjiInfo.time = hour + ":" + minutes;
            self.setData({
              shangjiInfo:res.data.retData
            })
          wx.setStorageSync("shangjiInfo", res.data.retData);
        }else{
          wx.showToast({
            title: res.data.retMsg,
            icon:'none'
          })
        }
      }
    });
  },
  getXiaji: function () {

    wx.request({
      url: app.globalData.apiUrl + "IS00/Login.ashx?Method=IdentityCheck",
      data: {
        LoginCode: username,
        LoginPassword: password,
        DeviceCode: code
      },
      method: 'GET',
      success: function (res) {
        console.log(res);
        if (res.data.state == 2) {
          wx.showToast({
            title: '下机操作成功',
          })
          wx.setStorageSync("shangjiInfo", null);
          self.setData({
            shangjiInfo:null
          })
        } else {
          wx.showToast({
            title: res.data.retMsg,
            icon: 'none'
          })
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      self = this;
      var shangjiInfo = wx.getStorageSync("shangjiInfo");
      if(shangjiInfo){
        self.setData({
          shangjiInfo:shangjiInfo
        })
      }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})